<?php

namespace IDock\Insurance\TPBX\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\TPBX\TPBXEnums;
use IDock\Insurance\TPBX\Response\AllTPBXResponse;
use IDock\Printer\VehicleModelPrinter;
use IDock\Printer\VehiclePrinter;

class VehicleModelResponse extends AllTPBXResponse
{

    /**
     * @var VehicleModelPrinter $printer
     */
    public $printer = VehicleModelPrinter::class;

    public function printerInit(): Response
    {
        $body = $this->getBody()['data'] ?? [];
        $modelList = $body['vehicleModelList'] ?? [];
        if (!$modelList) {
            return $this;
        }
        foreach ($modelList as $datum) {

            $vehiclePrinter = new VehiclePrinter();
            $vehiclePrinter->vehicleModelCode = $datum['autoModelCode'] ?? '';
            $vehiclePrinter->brandName = $datum['vehicleBrand'] ?? '';
            $vehiclePrinter->vehicleExhaust = $datum['exhaustCapability'] ?? '';
            //进口车标识
            $vehicleType = $datum['importFlag'] == '1' ? 'I1' : 'I2';
            $vehiclePrinter->vehicleImport = $vehicleType;
            $vehiclePrinter->vehicleImportName = IDockEnums::$importFlag[$vehicleType];
            $vehiclePrinter->vehicleName = $datum['standardName'] ?? '';
            $vehiclePrinter->vehiclePrice = $datum['purchasePriceTax'] ?? '';
            $vehiclePrinter->vehiclePriceWithOutTax = $datum['purchasePrice'] ?? '';
            $vehiclePrinter->vehicleSeat = $datum['vehicleSeats'] ?? '';
            $vehiclePrinter->remark = $datum['remark'] ?? '';
            $vehiclePrinter->power = $datum['power'] ?? '';
            $firstSaleDate = $datum['firstSaleDate'] ?? '';
            if($firstSaleDate){
                $vehiclePrinter->vehicleYear = date('Y-m-d',strtotime($datum['firstSaleDate']));
            }
            $tonCount = $datum['vehicleTonnages'] ?? 0;
            $vehiclePrinter->vehicleTonnage = floatval($tonCount) * 1000;
            $vehiclePrinter->wholeWeight = $datum['weight'] ? round($datum['weight'] * 1000,2) : '0';

            //能源类型
            $powerTypeCode = $datum['fuelType'] ?? '0';
            $vehiclePrinter->energyTypeCode =  array_search($powerTypeCode,TPBXEnums::$energyType)  ?: 'E00';
            $vehiclePrinter->energyTypeName = IDockEnums::$energyType[$vehiclePrinter->energyTypeCode];
            //新能源
            if (in_array($vehiclePrinter->energyTypeCode,['E02','E03','E04'])) {
                $vehiclePrinter->isNewEnergy = '1';
            }
            $this->printer->setModelList($vehiclePrinter);
        }

        return $this;
    }
}
